import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { EQDto, EQGetFiltersBySegmentFreqDto, EQGetFiltersByTempEQDto, EQIndexListDto, EQIndexOpenListDto, EQShowOpenDto, EQTop10ListDto, EQUpdateDto } from './eq.dto';
import { ApiPaginatedOkResponse } from '@/common/pager-response/api-paginated-ok-response.decorator';
import { UserModel } from '../user/user.model';
import { User } from '@/common/auth/user.decorator';
import { getPager } from '@/common/pager-response';
import { PageResult } from 'common/interface';
import { EQGetFiltersBySegmentFreqResult, EQGetFiltersByTempEQResult, EQOpenResult, EQOpenShowResult, EQResult, EQShowResult, EQTop10Result } from './eq.result';
import { EQService } from './eq.service';
import { UserAuthGuard } from '@/common/auth/user-auth.guard';

@ApiTags('EQ')
@Controller('/v1/eq')
export class EqV1Controller {
  constructor(private readonly eqService: EQService) { }

  @Get('/')
  @ApiOperation({ summary: '查询EQ列表' })
  @ApiPaginatedOkResponse(EQResult)
  @UseGuards(UserAuthGuard)
  async index(
    @Query() query: EQIndexListDto,
    @User() user: UserModel,
  ): Promise<PageResult<EQResult>> {
    const res = await this.eqService.index(query, user.uuid);
    return getPager(res[0], res[1]);
  }

  @Get('/open')
  @ApiOperation({ summary: '查询公开的EQ' })
  @ApiPaginatedOkResponse(EQOpenResult)
  async indexOpen(
    @Query() query: EQIndexOpenListDto,
  ): Promise<PageResult<EQOpenResult>> {
    const res = await this.eqService.indexOpen(query);
    return getPager(res[0], res[1]);
  }

  @Get('/:uuid')
  @ApiOperation({ summary: '查询EQ' })
  @ApiOkResponse({ type: EQShowResult })
  @UseGuards(UserAuthGuard)
  async show(
    @Param('uuid') uuid: string,
    @User() user: UserModel,
  ): Promise<EQShowResult | null> {
    return this.eqService.show(uuid, user.uuid);
  }

  @Get('/open/:uuid')
  @ApiOperation({ summary: '查询EQ' })
  @ApiOkResponse({ type: EQOpenShowResult })
  async showOpen(
    @Param('uuid') uuid: string,
    @Query() query: EQShowOpenDto,
  ): Promise<EQOpenShowResult | null> {
    return this.eqService.showOpen(uuid, query);
  }

  @Post('/get-filters-by-temp-eq')
  @ApiOperation({ summary: '固定目标曲线，切换原始耳机和滤波器获取滤波器' })
  @ApiOkResponse({ type: EQGetFiltersByTempEQResult })
  @UseGuards(UserAuthGuard)
  async getFiltersByTempEQ(
    @Body() body: EQGetFiltersByTempEQDto,
  ): Promise<EQGetFiltersByTempEQResult> {
    return this.eqService.getFiltersByTempEQ(body);
  }

  @Post('/get-filters-by-segment-freq')
  @ApiOperation({ summary: '通过分段频响，获取滤波器' })
  @ApiOkResponse({ type: EQGetFiltersBySegmentFreqResult })
  @UseGuards(UserAuthGuard)
  async getFiltersBySegmentFreq(
    @Body() body: EQGetFiltersBySegmentFreqDto,
  ): Promise<EQGetFiltersBySegmentFreqResult> {
    return this.eqService.getFiltersBySegmentFreq(body);
  }

  @Post('/')
  @ApiOperation({ summary: '创建EQ' })
  @ApiOkResponse({ type: EQResult })
  @UseGuards(UserAuthGuard)
  async create(
    @Body() body: EQDto,
    @User() user: UserModel,
  ): Promise<EQResult> {
    return await this.eqService.create(body, user.uuid);
  }

  @Patch('/:uuid')
  @ApiOperation({ summary: '更新EQ' })
  @ApiOkResponse({ type: EQResult })
  @UseGuards(UserAuthGuard)
  async update(
    @Body() body: EQUpdateDto,
    @Param('uuid') uuid: string,
    @User() user: UserModel,
  ): Promise<EQResult | null> {
    return this.eqService.update(body, uuid, user.uuid);
  }

  @Delete('/:uuid')
  @ApiOperation({ summary: '删除EQ' })
  @UseGuards(UserAuthGuard)
  async delete(
    @Param('uuid') uuid: string,
    @User() user: UserModel,
  ): Promise<void> {
    await this.eqService.delete(uuid, user.uuid);
  }

  @Post('/top10')
  @ApiOkResponse({ type: EQTop10Result, isArray: true })
  @ApiOperation({ summary: '与当前频响曲线相似的耳机' })
  @UseGuards(UserAuthGuard)
  async top10(
    @Body() body: EQTop10ListDto,
  ): Promise<EQTop10Result[]> {
    const res = await this.eqService.top10(body);
    return res;
  }
}
